Python: Give a class its own `self` at instantiation time

Posted by SuperDisk on Stack Overflow See other posts from Stack Overflow or by SuperDisk
Published on 2012-11-20T22:42:50Z Indexed on 2012/11/20 23:00 UTC
Read the original article Hit count: 120

Filed under:
|
|
|

I've got a button class that you can instantiate like so:

engine.createElement((0, 0), Button(code=print, args=("Stuff!",)))

And when it is clicked it will print "Stuff!". However, I need the button to destroy itself whenever it is clicked. Something like this:

engine.createElement((0, 0), Button(code=engine.killElement, args=(self,)))

However, that would just kill the caller, because self refers to the caller at that moment. What I need to do is give the class its own 'self' in advance...

I thought of just making the string 'self' refer to the self variable upon click, but what if I wanted to use the string 'self' in the arguments?

What is the way to do this? Is my architecture all wrong or something?

Thanks.

© Stack Overflow or respective owner

Related posts about python

Related posts about class